﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.ModelBinding;
//este es para el json
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Ajax.Utilities;
using Microsoft.AspNet.FriendlyUrls;
using Microsoft.Practices.Unity;
using Triunfadores.BusinessEntities;
using Triunfadores.BusinessLogic.Service;

namespace Triunfadores.Web.Clientes
{
    public partial class Registrar : System.Web.UI.Page
    {
        #region Declaracion de Dependencias
        [Dependency]
        public IBeneficiarioService BeneficiarioService { get; set; }

        [Dependency]
        public IUbigeoService UbigeoService { get; set; }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            var accion = Request.QueryString["opc"];
            var id = Request.QueryString["idBeneficiario"];
            
            if (accion != null && accion == "nuevo")
            {
                fvBeneficiario.DefaultMode = FormViewMode.Insert;
            }
            else if (accion != null && accion == "editar")
            {
                //var idCliente = Int32.Parse(id);
                //var cliente = ClienteService.GetClienteById(idCliente);

                fvBeneficiario.DefaultMode = FormViewMode.Edit;
            }
            else if (accion != null && id != null && accion == "delete")
            {
                //Int32 idAmbiente = Int32.Parse(id);
            }

        }

        #region Consultas Get
        //traer tipos de clientes
        public IEnumerable<Ubigeo> GetUbigeos()
        {
            var ubigeos = UbigeoService.GetAllUbigeos().ToList();

            ubigeos.Insert(0, new Ubigeo() { Id = 0, NombreCompleto = "[- Selecciona -]" });

            return ubigeos.AsEnumerable();
        }

        //traer clientes por Id
        public Beneficiario GetBeneficiarioByrId([QueryString("idBeneficiario")] Int32? id)
        {
            if (id.HasValue)
            {
                return BeneficiarioService.GetBeneficiarioById(id.Value);
            }

            return null;
        }

        #endregion



        //Registrar Benefiario
        public void RegistrarBeneficiario(Beneficiario beneficiario)
        {
            //if (ModelState.IsValid)
            BeneficiarioService.AddBeneficiario(beneficiario);

            string script = "alert('Benefiario registrado con exito.');";
            string redirectURL = Page.ResolveClientUrl("GestBeneficiarios.aspx");
            ScriptManager.RegisterStartupScript(Page, GetType(), "alerta", 
                                                  script+"document.location = '" + redirectURL + "';", true);

        }

        public void UpdateBeneficiario(Beneficiario beneficiario)
        {
            //if (ModelState.IsValid)
            BeneficiarioService.UpdateBeneficiario(beneficiario);

            string script = "alert('Beneficiario modificado con exito.');";
            string redirectURL = Page.ResolveClientUrl("GestBeneficiarios.aspx");
            ScriptManager.RegisterStartupScript(Page, GetType(), "alerta",
                                                  script + "document.location = '" + redirectURL + "';", true);
        }

        

    }
}